package com.situ.blog.controller;

import com.situ.blog.pojo.entity.Auth;
import com.situ.blog.pojo.query.AuthQuery;
import com.situ.blog.service.IAuthService;
import com.situ.blog.service.impl.AuthServiceImpl;
import com.situ.blog.util.MyBeanUtils;
import com.situ.blog.util.PageInfo;
import com.situ.blog.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/auth")
public class AuthController extends BaseController{
    private IAuthService authService = new AuthServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        switch (method){
            case "selectByPage":
                selectByPage(req,resp);
                break;
            case "selectByRole":
                selectByRole(req,resp);
                break;
            case "deleteById":
                deleteById(req,resp);
                break;
            case "deleteAll":
                deleteAll(req,resp);
                break;
            case "add":
                add(req,resp);
                break;
            case "selectById":
                selectById(req,resp);
                break;
            case "update":
                update(req,resp);
                break;
        }
    }

    private void selectByRole(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("AuthController.selectByRole");
        String role = req.getParameter("role");
        List<Auth> authList = authService.selectByRole(Integer.parseInt(role));
        toJSON(resp,Result.ok(authList));
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("AuthController.update");
        Auth auth = MyBeanUtils.copyToBean(req,Auth.class);
        authService.update(auth);
        toJSON(resp,Result.ok("更新成功"));
    }

    private void selectById(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("AuthController.selectById");
        String id = req.getParameter("id");
        Auth auth = authService.selectById(Integer.parseInt(id));
        toJSON(resp,Result.ok(auth));

    }

    private void add(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("AuthController.add");
        Auth auth = MyBeanUtils.copyToBean(req,Auth.class);
        authService.add(auth);
        toJSON(resp,Result.ok("添加成功"));
    }

    private void deleteAll(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("AuthController.deleteAll");
        String[] ids = req.getParameterValues("ids");
        authService.deleteAll(ids);
        toJSON(resp,Result.ok("删除成功"));
    }

    private void deleteById(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("AuthController.deleteById");
        String id = req.getParameter("id");
        authService.deleteById(Integer.parseInt(id));
        toJSON(resp,Result.ok("删除成功"));
    }

    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("AuthController.selectByPage");
        AuthQuery authQuery = MyBeanUtils.copyToBean(req,AuthQuery.class);
        PageInfo pageInfo = authService.selectByPage(authQuery);
        toJSON(resp, pageInfo);

    }


}
